<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Workflow_desc extends Model
{
    //
	protected $table='workflow_desc';
	protected $guarded=[];
	protected $hidden=[];

	public function setStartTimeAttribute($value)
	{
		$this->attributes['start_time'] = is_int($value) ? $value : strtotime($value);
	}

	public function getStartTimeAttribute()
	{
		return date('Y-m-d', $this->attributes['start_time']);
	}
	public function Workflow_jl(){
		return $this->belongsTo('App\Model\Workflow_jl','lovel_uid','approver_uid');
	}


//	移动管理端排班审批列表接口
	public function Scheduling_lists($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>1])
			->with('Workflow_jl')->with(['Scheduling'=>function($query){
				$query->with('store');
			}])->get();
	}
//	移动管理端中止审批已审批列表接口
	public function Leave_lists($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>2])->where('status','!=',1)
			->with('Workflow_jl')->with(['Leave'=>function($query){
				$query->with('staff_info')->with('store')->with('store_station_')->with('leave_type');
			}])->paginate(config('app.page'));
	}
//	移动管理端中止审批待审批列表接口
	public function Leave_lists_no($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>2,'status'=>1])
			->with('Workflow_jl')->with(['Leave'=>function($query){
				$query->with('staff_info')->with('store')->with('store_station_')->with('leave_type');
			}])->paginate(config('app.page'));
	}

	//	移动管理端临时调动已审批列表接口
	public function move_lists($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>3])->where('status','!=',1)
			->with('Workflow_jl')->with(['Move_task'=>function($query){
				$query->with('store')->with('store_station')->with('from_staff')
					->with('from_store');
			}])->paginate(config('app.page'));
	}
	//	移动管理端临时调动待审批列表接口
	public function move_lists_no($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>3,'status'=>1])
			->with('Workflow_jl')->with(['Move_task'=>function($query){
				$query->with('store')->with('store_station')->with('from_staff')
					->with('from_store');
			}])->paginate(config('app.page'));
	}

	public function stop_list($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>4])->where('status','!=',1)->with('userinfo')
			->with('Workflow_jl')->with(['stop_settle'=>function($query){
				$query->with(['staff_task'=>function($query){
					$query->with('store_station');
				}]);
			}])->get();
	}
	public function stop_list_no($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>4,'status'=>1])
			->with('userinfo')->with('Workflow_jl')->with(['stop_settle'=>function($query){
				$query->with(['staff_task'=>function($query){
					$query->with('store_station');
				}]);
			}])->get();
	}

	//	店长端终止结算列表接口
	public function stop_list_store($uid){
		return $data=$this->where(['lovel_uid'=>$uid,'type'=>4,'status'=>1])
			->with('Workflow_jl')->with(['Move_task'=>function ($query){
				$query->with('store_station');
				$query->with('staff_info');
			}])->with(['stop_settle'=>function($query){
				$query->with(['staff_task'=>function($query){
					$query->with('store_station');
				}]);
			}])->get();
	}

	public function descData($id){
		return $this->where(['id'=>$id])->first();
	}


	public function stop_settle(){
		return $this->belongsTo('App\Model\Stop_settle','stop_id');
	}
	public function Scheduling(){
		return $this->belongsTo('App\Model\Scheduling','settlement_id');
	}
	public function Leave(){
		return $this->belongsTo('App\Model\Leave','leave_id');
	}
	public function Move_task(){
		return $this->belongsTo('App\Model\Move_task','move_task_id');
	}

//	根据终止id查询申请审核流
	public function stopData($stop_id,$uid){
		return $this->where(['stop_id'=>$stop_id,'lovel_uid'=>$uid])->first();
	}

	public function searchWorkflow($uid,$stop_id){
		return $this->where(['lovel_uid'=>$uid,'stop_id'=>$stop_id,'status'=>1])->first();
	}

	public function userinfo(){
		return $this->belongsTo('App\Model\Users','lovel_uid');
	}




}
